#!/bin/bash
#

function check_rabbitmq() {
    netstat -tunlp | grep '5672' | grep -v -E "15672|25672" &> /dev/null
    REVTAL=$?
    
    if [ $REVTAL -eq 0 ]; then 
        rabbitmq_pid=`netstat -tunlp | grep '5672' | grep -v -E "15672|25672" | awk '{print $NF}' | awk -F '/' '{print $1}'`
        echo "$(date +%Y-%m-%d" "%H:%M:%S) - - rabbitmq service is running: ${rabbitmq_pid}."
    else
        nohup /otp/mq/rabbitmq/sbin/rabbitmq-server start >> /otp/mq/rabbitmq/logs/rabbitmq-server.log &
        echo "rabbitmq service start."
    fi
}


while true; do
    check_rabbitmq
    sleep 120s
done